/**
 * 
 */
package denaris.core;

import denaris.core.controller.AbstractController;
import denaris.core.entity.AbstractEntity;
import denaris.midlet.GameScreen;

/**
 * @author stkl23
 *
 */
public class NinjaEmitter extends BulletEmitter {

	private class NinjaController extends AbstractController{
		private int deltaX;
		private int deltaY;
		private int delay;
		
		public void reset() {
			changeDirection();
		}
		
		private void changeDirection() {
			deltaX = GameScreen.RANDOM.nextInt() % 30;
			deltaY = GameScreen.RANDOM.nextInt() % 30;
			delay = GameScreen.nextPositiveInt(4);
		}
		
		public boolean advance() {
			boolean result = true;
			controllable.move(deltaX, deltaY);
			if (!isInScope(controllable.getX(), controllable.getY())) {
				result = false;
			}
			
			if (delay == 0) {
				changeDirection();
			}else {
				delay--;
			}
			
			return result;
		}
	}
	
	public NinjaEmitter(int cacheSize, int dx, int dy, String bulletImgPath, int bulletWidth, int bulletHeight) {
		super(cacheSize, dx, dy, bulletImgPath, bulletWidth, bulletHeight);
	}

	/* (non-Javadoc)
	 * @see denaris.core.BulletEmitter#collidesWith(denaris.core.AbstractParticle, denaris.core.entity.AbstractEntity)
	 */
	public boolean collidesWith(AbstractParticle particle, AbstractEntity entity) {
		return particle.getController().getSprite().collidesWith(entity.getController().getSprite(), false);
	}

	/* (non-Javadoc)
	 * @see denaris.core.BulletEmitter#createController()
	 */
	public AbstractController createController() {
		return new NinjaController();
	}
	
	public void emitt(int x, int y) {
		x += 30;
		y += 8;
		super.emitt(x, y);
	}

	public boolean collidesWith(AbstractParticle particle, TiledMap map) {
		return particle.getController().getSprite().collidesWith(map.getLayer(), false);
	}
}